/**
 * 登录模块
 * 功能：登录
 */
cc.Class({
    extends: cc.Component,

    properties: {
        edAccount: cc.EditBox,
        edPassword: cc.EditBox,
        fbRegister: cc.Prefab, 
        fbRegisterVenify: cc.Prefab, 
        fbForgetPassword: cc.Prefab,
        fbResetPassword: cc.Prefab,
        _callBack:null,
        _wxLogin:false
    },

    setCallBack: function(callBack)
    {
        this._callBack = callBack;
    },

    // use this for initialization
    onLoad: function () {  
        if(User.getAppState() == 0)
        {
            var content = this.node.getChildByName('content');
            content.getChildByName("btnQQLogin").active = false;
            content.getChildByName("btnWeichatLogin").active = false;
            content.getChildByName("labDec").active = false;
            content.getChildByName("spLine").active = false;
        }

        if(User.getTel() != "");
        {
            this.edAccount.string = User.getTel();
        }

        window.Notification.on("onRegisterVerify",function(data){
            this.onRegisterVerify(data);
        },this);
        window.Notification.on("onResetPassword",function(data){
            this.onResetPassword(data);
        },this);
        window.Notification.on("onPasswordSuccess",function(data){
            this.onPasswordSuccess(data);
        },this);
        window.Notification.on("onCloseLogin",function(data){
            this.onCloseBtn(data);
        },this);
    },

    onTextChangePwd:function(text, editbox, customEventData){
        var txt = text;
        if(txt == "")
        {
            this.pwd = txt;
        }
        else
        {
            var re = txt.replace(/\s+/g,"");
            editbox.string = re;
            txt = re;
            this.pwd = txt;
        }
    },

    onTextChangedPhone:function(text, editbox, customEventData){
        var txt = text;
        if(Utils.isInt(txt) || txt == "")
            this.phoneNum = txt;
        else
        {
            txt = this.phoneNum;
            editbox.string = txt;
            return;
        }
    },


    //登录
    onLogin: function(){
        if(Utils.checkInput(this.edAccount.string,this.edPassword.string,null,null,true))
        {
            var self = this;
            var recv = function recv(ret) {
                ComponentsUtils.unblock();
                if (ret.Code !== 0 && ret.Code !== 100) {
                        cc.error(ret.Msg);
                        ComponentsUtils.showTips(ret.Msg);
                    } else {
                        User.upDateUserInfo(false,ret.Data);
                        User.setLoginToken(ret.Data.Token);
                        User.setIsvermify(true);
                        User.setLoginMode(2);
                        User.setPwd(this.edPassword.string);
                        User.setUid(ret.Data.Uid);
    
                        if(ret.Code !== 100)
                        {
                            CL.MAINMANAGE.onLoginSK();
                        }
                            
                        ComponentsUtils.showTips("登录成功");
                        this.onCloseBtn(true);
                    }
                }.bind(this);

                var data = {
                    Equipment: Utils.getEquipment(),
                    SourceType: Utils.getSourceType(),
                    Mobile:this.edAccount.string,
                    Pwd:this.edPassword.string
                }; 
            CL.HTTP.sendRequestRet(DEFINE.HTTP_MESSAGE.ACCOUNTLOGIN, data, recv.bind(this),"POST");   
            ComponentsUtils.block();  
        }
    },

    //QQ登录
    onQQLogin:function(){
        if(cc.sys.isNative)
        {
            var callback = function(obj){
                var isSuccessStr = obj.isSuccessStr;
                var type = obj.type;
                var openid = obj.openid;
                var access_token = obj.access_token;
                //cc.log("doQQLogin, isSuccessStr:"+isSuccessStr+",openid:"+openid+",access_token:"+access_token);
                
                if(isSuccessStr == "1"){
                    this.doSdkLoginCallback(isSuccessStr, type, openid, access_token);
                }
                else{
                    ComponentsUtils.showTips("QQ登录失败！");
                }

            }.bind(this);
            //PlatformUtils.doLoginThirdPartSDK("1", callback);
        }
        else
            ComponentsUtils.showTips("暂时无法QQ登录！");
    },

    //微信登录
    onWechatLogin:function(){
        if(cc.sys.isNative)
        {
            if(this._wxLogin)
                return;
            this._wxLogin = true;
            var callback = function(obj){
                this._wxLogin = false;
                var isSuccessStr = obj.isSuccessStr;
                var type = obj.type;
                var openid = obj.openid;
                var access_token = obj.access_token;
                //cc.log("login_login doWechatLogin, isSuccessStr:"+isSuccessStr+",openid:"+openid+",access_token:"+access_token);
                
                if(isSuccessStr == "1"){
                    this.doSdkLoginCallback(isSuccessStr, type, openid, access_token);
                }
                else{
                    cc.error("微信登录失败！");
                    ComponentsUtils.showTips("微信登录失败！");
                }

            }.bind(this);
            //PlatformUtils.doLoginThirdPartSDK("2", callback);
        }
        else
            ComponentsUtils.showTips("暂时无法微信登录！");
    },

    doSdkLoginCallback:function(isSuccessStr, type, openid, access_token){
        var code = "";
        var mode = 0;
        if(type == "1"){//QQ
            code = access_token;
            mode = 4;
        }else if(type == "2"){//weichat
            code = openid;
            mode = 3;
        }
        var sourcetype = "1";
        var type = type;
        //ComponentsUtils.block();
        var self = this;
        var recv = function(ret){
            cc.info("login_login interLoginRet:" + ret);
            ComponentsUtils.unblock();
            if (ret.Code !== 0 && ret.Code !=100) {
                cc.error(ret.Msg);
                //ComponentsUtils.showTips(ret.Msg+":"+ ret.Code);
            }else{
                //ComponentsUtils.showTips("登录成功");
                User.upDateUserInfo(false,ret.Data);
                User.setLoginToken(ret.Data.Token);//令牌
                User.setIsvermify(true);//验证通过
                User.setLoginMode(mode);//登录模式
                User.setUid(ret.Data.Uid);
              
                if(ret.Code !== 100)
                {
                    CL.MAINMANAGE.onLoginSK();
                }
                this.onCloseBtn(true);
           }
        }.bind(this);
    
        var data = {
            Equipment: Utils.getEquipment(),
            Code: code,
            SourceType:Utils.getSourceType(),
            LoginType:type
        };

        //cc.log("第三方登录 equipment:" + Utils.getEquipment() + "code:"+code);
        CL.HTTP.sendRequest(DEFINE.HTTP_MESSAGE.OTHERLOGIN, data, recv.bind(this),"POST"); 
        ComponentsUtils.block();

    },

    //忘记密码
    onForgetPassWord: function(data){
        var canvas = cc.find("Canvas");
        var fbForgetPasswordPop = cc.instantiate(this.fbForgetPassword);
        canvas.addChild(fbForgetPasswordPop);
    },

    //马上注册
    onRegister:function(data){
        var canvas = cc.find("Canvas");
        var fbRegisterPop = cc.instantiate(this.fbRegister);
        canvas.addChild(fbRegisterPop);
    },

    //注册验证
    onRegisterVerify:function(data){
        var canvas = cc.find("Canvas");
        var fbRegisterVenifyPop = cc.instantiate(this.fbRegisterVenify);
        fbRegisterVenifyPop.getComponent(fbRegisterVenifyPop.name).initData(data);
        canvas.addChild(fbRegisterVenifyPop);
    },  

    //重置密码
    onResetPassword:function(data){
        var canvas = cc.find("Canvas");
        var fbResetPasswordPop = cc.instantiate(this.fbResetPassword);
        fbResetPasswordPop.getComponent(fbResetPasswordPop.name).initData(data);
        canvas.addChild(fbResetPasswordPop);
    },  

    onPasswordSuccess:function(data){
        ComponentsUtils.showTips("密码修改成功");
    },  

    onClose:function(){
        window.Notification.offType("onRegisterVerify");
        window.Notification.offType("onResetPassword");
        window.Notification.offType("onPasswordSuccess");
        window.Notification.offType("onCloseLogin");
        this.node.getComponent("Page").backAndRemove();
        if(this._callBack != null)
            this._callBack(false);
    },

    onCloseBtn:function(data){
        window.Notification.offType("onRegisterVerify");
        window.Notification.offType("onResetPassword");
        window.Notification.offType("onPasswordSuccess");
        window.Notification.offType("onCloseLogin");
        if(this._callBack != null)
            this._callBack(true);
        this.node.getComponent("Page").backAndRemove();
    },  

});
